library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.1
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_REP <- NA
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 2] <- 1
summary(factor(mil_conf.df$DOV_REP))

mil_conf.df$DOV_DEM <- NA
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 3] <- 1
summary(factor(mil_conf.df$DOV_DEM))

mil_conf.df$DOV_PART <- NA
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 4] <- 1
summary(factor(mil_conf.df$DOV_PART))


# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$REPUB <- NA
df$REPUB[df$P_ASSIGN1 == 1] <- 0
df$REPUB[df$P_ASSIGN1 == 6] <- 1
summary(factor(df$REPUB))

df$DEMOC <- NA
df$DEMOC[df$P_ASSIGN1 == 1] <- 0
df$DEMOC[df$P_ASSIGN1 == 7] <- 1
summary(factor(df$DEMOC))

df$PARTISAN <- NA
df$PARTISAN[df$P_ASSIGN1 == 1] <- 0
df$PARTISAN[df$P_ASSIGN1 == 8] <- 1
summary(factor(df$PARTISAN))



# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

# Wave 1
# dov_assignment_a == 2 - REPUBLICAN OFFICERS
w1r <- svyttest(Q8a~DOV_REP, w1_design)

# dov 3 - DEMOCRAT OFFICERS
w1d <- svyttest(Q8a~DOV_DEM, w1_design)

# dov 4 - Partisan military OFFICERS
w1p <- svyttest(Q8a~DOV_PART, w1_design)


# Wave 2
# P_ASSIGN1 == 6 REPUBLICAN OFFICERS
w2r <- svyttest(Q11_d~REPUB, w2_design)

# P_ASSIGN1 == 7 DEMOCRAT OFFICERS
w2d <- svyttest(Q11_d~DEMOC, w2_design)

# P_ASSIGN1 == 8 Partisan military OFFICERS
w2p <- svyttest(Q11_d~PARTISAN, w2_design)


# Wave 1
pt.est1 <- c(w1r$estimate,
             w1d$estimate,
             w1p$estimate)
ci.low1 <- c(w1r$conf.int[1],
             w1d$conf.int[1],
             w1p$conf.int[1])
ci.high1 <- c(w1r$conf.int[2],
              w1d$conf.int[2],
              w1p$conf.int[2])
plot.labs <- c("Republican\nMilitary",
               "Democrat\nMilitary",
               "Partisan\nMilitary")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                      lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 1")+
  theme_bw()

# Wave 2
pt.est2 <- c(w2r$estimate,
             w2d$estimate,
             w2p$estimate)
ci.low2 <- c(w2r$conf.int[1],
             w2d$conf.int[1],
             w2p$conf.int[1])
ci.high2 <- c(w2r$conf.int[2],
              w2d$conf.int[2],
              w2p$conf.int[2])
plot.labs <- c("Republican\nMilitary",
               "Democrat\nMilitary",
               "Partisan\nMilitary")
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

grid.arrange(plot_w1, plot_w2, nrow=1, ncol=2)
